package com.common.service.judicialpersonal.impl;

import com.common.converter.JobExplainConverter;
import com.common.entity.job.JobExplainEntity;
import com.common.entity.personal.PersonalEntity;
import com.common.global.BizException;
import com.common.global.CommonEnum;
import com.common.global.BizServiceEnum;
import com.common.repository.judicialpersonal.JobExplainRepository;
import com.common.repository.judicialpersonal.PersonalRepository;
import com.common.requestvo.judicialpersonal.JobExplainReqVo;
import com.common.service.judicialpersonal.JobExplainService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.transaction.Transactional;

/**
 * JobExplain 岗位说明书 ServiceImpl
 *
 * @author johnny
 * @create 2021-05-14 8:31 下午
 **/
@Service
@Slf4j
@Transactional(rollbackOn = Exception.class)
public class JobExplainServiceImpl implements JobExplainService {

    @Autowired
    private JobExplainRepository jobExplainRepository;

    @Autowired
    private PersonalRepository personalRepository;

    @Override
    public void add(JobExplainReqVo jobExplainReqVo) {
        jobExplainReqVo.setId(null);
        Long personalId = jobExplainReqVo.getPersonalId();
        PersonalEntity personalEntity = personalRepository.findById(personalId).orElse(null);
        if (personalEntity != null) {
            if (personalEntity.getDepartmentId() == null) {
                throw new BizException(BizServiceEnum.DEPARTMENT_NOT_FIND);
            }
            if (jobExplainReqVo.getJobId() == null) {
                throw new BizException(BizServiceEnum.JOB_NO_EXIST);
            }
            Long count = jobExplainRepository.countByPersonalId(personalId);
            //岗位说明书 1个档案只能添加1个
            if (count == 0) {
                JobExplainEntity jobExplainEntity = JobExplainConverter.INSTANCE.vo2domain(jobExplainReqVo);
                //部门通过 人员的 部门获取
                jobExplainEntity.setDepartmentId(personalEntity.getDepartmentId());
                jobExplainRepository.save(jobExplainEntity);
                personalEntity.setJobId(jobExplainEntity.getJobId());
                personalRepository.save(personalEntity);
            } else {
                throw new BizException(BizServiceEnum.JOB_EXPLAIN_EXIST);
            }
        }

    }

    @Override
    public void edit(JobExplainReqVo jobExplainReqVo) {
        if (jobExplainReqVo.getId() != null && jobExplainReqVo.getId() != 0L) {
            PersonalEntity personalEntity = personalRepository.findById(jobExplainReqVo.getPersonalId()).orElse(null);
            if (personalEntity != null) {
                if (personalEntity.getDepartmentId() == null) {
                    throw new BizException(BizServiceEnum.DEPARTMENT_NOT_FIND);
                }
                if (jobExplainReqVo.getJobId() == null) {
                    throw new BizException(BizServiceEnum.JOB_NO_EXIST);
                }
                if (jobExplainRepository.existsById(jobExplainReqVo.getId())) {
                    JobExplainEntity jobExplainEntity = JobExplainConverter.INSTANCE.vo2domain(jobExplainReqVo);
                    jobExplainRepository.save(jobExplainEntity);
                    personalEntity.setJobId(jobExplainEntity.getJobId());
                    personalRepository.save(personalEntity);
                } else {
                    throw new BizException(CommonEnum.ID_NOT_FIND_ENTITY);
                }
            }
        } else {
            throw new BizException(CommonEnum.ID_NOT_NULL);
        }
    }

    @Override
    public void delete(Long id) {
        if (jobExplainRepository.existsById(id)) {
            jobExplainRepository.deleteById(id);
        } else {
            throw new BizException(CommonEnum.ID_NOT_FIND_ENTITY);
        }
    }
}